package com.herench.bunch.clinic.convert;

import ca.uhn.hl7v2.model.Message;
import ca.uhn.hl7v2.model.v24.group.ADR_A19_QUERY_RESPONSE;
import ca.uhn.hl7v2.model.v24.message.ADR_A19;
import ca.uhn.hl7v2.model.v24.message.QRY_A19;
import ca.uhn.hl7v2.model.v24.segment.*;
import com.herench.bunch.clinic.handler.ProxyMsgCodeConstant;
import com.herench.bunch.clinic.support.ProxyMsgTemplate;
import com.herench.bunch.commons.support.RequestMessage;
import org.dom4j.Document;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.Assert;

public class EstablishPatientConvert extends AbstractMessageConvert {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public Document doToMessage(RequestMessage requestMessage) throws Exception {
        Document inMessage = ProxyMsgTemplate.getInMsgTpl(ProxyMsgCodeConstant.ESTABLISH_PATIENT_TRADE_CODE);
        Element requestElement = inMessage.getRootElement();
        QRY_A19 hl7 = (QRY_A19) requestMessage.getHl7Message();
        QRF qrf = hl7.getQRF();

        String cardNo = qrf.getQrf1_WhereSubjectFilter(0).getValue();
        Assert.notNull(cardNo,"cardNo 不能为空.");
        String CardType = qrf.getQrf1_WhereSubjectFilter(1).getValue();
        Assert.notNull(CardType,"CardType 不能为空.");
        String PatientName = qrf.getQrf1_WhereSubjectFilter(2).getValue();
        String Birthday = qrf.getQrf1_WhereSubjectFilter(3).getValue();
        String Sex = qrf.getQrf1_WhereSubjectFilter(4).getValue();
        String Address = qrf.getQrf1_WhereSubjectFilter(5).getValue();
        String OperType = qrf.getQrf1_WhereSubjectFilter(6).getValue();
        Assert.notNull(OperType,"OperType 不能为空.");
        String IDNumber = qrf.getQrf1_WhereSubjectFilter(7).getValue();
        String MedicareID = qrf.getQrf1_WhereSubjectFilter(8).getValue();
        String CureCardNo = qrf.getQrf1_WhereSubjectFilter(9).getValue();
        String OtherCardID = qrf.getQrf1_WhereSubjectFilter(10).getValue();
        String Phone = qrf.getQrf1_WhereSubjectFilter(11).getValue();
        String PatientType = qrf.getQrf1_WhereSubjectFilter(12).getValue();
        String PhoneNo = qrf.getQrf1_WhereSubjectFilter(13).getValue();

        requestElement.addElement("CardNo").addText(cardNo);
        requestElement.addElement("CardType").addText(CardType);
        requestElement.addElement("PatientName").addText(PatientName!=null?PatientName:"");
        requestElement.addElement("Birthday").addText(Birthday!=null?Birthday:"");
        requestElement.addElement("Sex").addText(Sex!=null?Sex:"");
        requestElement.addElement("Address").addText(Address!=null?Address:"");
        requestElement.addElement("OperType").addText(OperType);
        requestElement.addElement("IDNumber").addText(IDNumber!=null?IDNumber:"");
        requestElement.addElement("MedicareID").addText(MedicareID!=null?MedicareID:"");
        requestElement.addElement("CureCardNo").addText(CureCardNo!=null?CureCardNo:"");
        requestElement.addElement("OtherCardID").addText(OtherCardID!=null?OtherCardID:"");
        requestElement.addElement("Phone").addText(Phone!=null?Phone:"");
        requestElement.addElement("PatientType").addText(PatientType!=null?PatientType:"");
        requestElement.addElement("PhoneNo").addText(PhoneNo!=null?PhoneNo:"");
        String res = inMessage.asXML();
        logger.info("200100 to HIS XML = "+ res);
        return inMessage;
    }

    @Override
    public Message doFromMessage(String message) throws Exception {
        Assert.notNull(message, "message 为空");
        logger.info("200100 from HIS XML = "+ message);
        Document doc = ProxyMsgTemplate.getOutMsgTpl(message);
        ADR_A19 adrA19 = new ADR_A19();
        adrA19.initQuickstart("ADR_A19", "ADR_A19", "P");
        MSH msh = adrA19.getMSH();
        initResponseMessageMSH(msh);

        MSA msa = adrA19.getMSA();
        msa.getErrorCondition().getCe1_Identifier().setValue(doc.getRootElement().elementTextTrim("IsSuccessed"));
        msa.getErrorCondition().getCe2_Text().setValue(doc.getRootElement().elementTextTrim("ErrorMsg"));


        QRD qrd = adrA19.getQRD();
        qrd.getQrd8_WhoSubjectFilter();
        Element request = doc.getRootElement();
        ADR_A19_QUERY_RESPONSE queryResponse = adrA19.getQUERY_RESPONSE(0);
        PID pid = queryResponse.getPID();

        pid.getPid1_SetIDPID().setValue(request.elementTextTrim("HISPatientID"));
        pid.getPid2_PatientID().getCx1_ID().setValue(request.elementTextTrim("CureCardNo"));
        pid.getPid2_PatientID().getCx2_CheckDigit().setValue(request.elementTextTrim("MedCardNo"));
        pid.getPid2_PatientID().getCx3_CodeIdentifyingTheCheckDigitSchemeEmployed().setValue(request.elementTextTrim("IDCardNo"));
        pid.getPid2_PatientID().getCx5_IdentifierTypeCode().setValue(request.elementTextTrim("GIDCardNo"));
        pid.getPid2_PatientID().getCx6_AssigningFacility().getHd1_NamespaceID().setValue(request.elementTextTrim("HealthCardNo"));
        pid.getPid2_PatientID().getCx6_AssigningFacility().getHd2_UniversalID().setValue(request.elementTextTrim("Balance"));
        pid.getPid2_PatientID().getCx6_AssigningFacility().getHd3_UniversalIDType().setValue(request.elementTextTrim("CardStatus"));

        pid.getPid5_PatientName(0).getXpn2_GivenName().setValue(request.elementTextTrim("PatientName"));
        pid.getPid7_DateTimeOfBirth().getTs1_TimeOfAnEvent().setValue(request.elementTextTrim("Birthday"));
        pid.getPid7_DateTimeOfBirth().getTs2_DegreeOfPrecision().setValue(request.elementTextTrim("PatientAge"));
        pid.getPid8_AdministrativeSex().setValue(request.elementTextTrim("PatientSex"));
        pid.getPid11_PatientAddress(0).getXad1_StreetAddress().getSad1_StreetOrMailingAddress().setValue(request.elementTextTrim("Address"));
        pid.getPid13_PhoneNumberHome(0).getXtn7_PhoneNumber().setValue(request.elementTextTrim("PhoneNo"));
        pid.getPid15_PrimaryLanguage().getCe1_Identifier().setValue(request.elementTextTrim("HISCreatedOn"));
        pid.getPid15_PrimaryLanguage().getCe2_Text().setValue(request.elementTextTrim("inPatientID"));
        pid.getPid15_PrimaryLanguage().getCe3_NameOfCodingSystem().setValue(request.elementTextTrim("MedRecordNO"));
        pid.getPid15_PrimaryLanguage().getCe4_AlternateIdentifier().setValue(request.elementTextTrim("Remark2"));

        return adrA19;
    }

    @Override
    public Message generatorReqMessage() throws Exception {




        return null;
    }
}
